﻿var assignerPageVue = new Vue({
    el: "#assignerPage",
    data: {
        equipmentList: [],              //所属设备列表
        maintainRecordList: [],         //所有保养记录
        statusDictionary: {},           //设备状态字典
        maintStatusDictionary: {},      //保养状态字典
        approvalStatusDictionary: {},   //签核状态字典
        batchRecord: {
            recordIdx: 0,
            recordIDs: [],
        },
        pageModel: {
            pageIdx: 1,
            pageSize: 10,
            pageTotal: 0,
            pageLength: 0,
            reults: []
        },
        searchModel: {
            queryStatus: 2,
            queryAssignerID: GlobalUser.getUserInfo().ID,
            queryManagerID: 0,
            queryIsNeedMaintain: 1,

            isAsc: true,
            isAll: false
        },
    },
    methods: {
        EditEquipment: function (equipmentID) {
            EditEquipmentDetail(this, equipmentID);
        },
        AddMaintainRecord: function (equipmentID) {
            AddMaintainFormRecord(this, equipmentID);
        },
        EditMaintainRecord: function (recordID) {
            EditMaintainFormRecord(this, recordID);
        },
        DeleteMaintainRecord: function (record) {
            var message = "确认删除confirm deletion【" + record.Equipment.BaseInfo.Code + "】保养记录Maintenance record【" + record.BaseInfo.ID + "】?";
            if (confirm(message)) {
                DeleteMaintainFormRecord(this, record.BaseInfo.ID, false);
            }
        },
        SubmitMaintainRecord: function (recordID) {
            SubmitMaintainFormRecord(this, recordID, false);
        },
        ApproveMaintainRecord: function (recordID) {
            ApproveMaintainFormRecord(this, recordID);
        },
        BatchSubmitMaintainRecord: function () {
            var vue = this;
            vue.batchRecord.recordIdx = 0;
            vue.batchRecord.recordIDs = [];
            
            $(".record-box").each(function () {
                if ($(this).prop("checked")) {
                    vue.batchRecord.recordIDs.push($(this).val());
                }
            });
            
            if (vue.batchRecord.recordIDs.length > 0) {
                BatchSubmitMaintainFormRecord(vue);
            } else {
                toastr.error("请至少选择一行数据Please select at least one row of data。");
            }
        },
        BatchDeleteMaintainRecord: function () {
            if (confirm("确认批量删除数据Confirm to delete data in batch?")) {
                var vue = this;
                vue.batchRecord.recordIdx = 0;
                vue.batchRecord.recordIDs = [];

                $(".record-box").each(function () {
                    if ($(this).prop("checked")) {
                        vue.batchRecord.recordIDs.push($(this).val());
                    }
                });

                if (vue.batchRecord.recordIDs.length > 0) {
                    BatchDeleteMaintainFormRecord(vue);
                } else {
                    toastr.error("请至少选择一行数据Please select at least one row of data。");
                }
            }
        },
        SelectAllMaintainRecord: function () {
            $(".record-box").prop("checked", $("#selectAllBox").prop("checked"));
        },
        GotoPage: function (toPageIdx) {
            this.pageModel.pageIdx = toPageIdx;
            ResetPageData(this);
        }
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        var vue = this;
        //页面加载后初始的数据
        this.statusDictionary = GlobalDictionary.GetEquipmentStatusDictionary();
        this.maintStatusDictionary = GlobalDictionary.GetMaintainStatusDictionary();
        this.approvalStatusDictionary = GlobalDictionary.GetApprovalStatusDictionary();
        ResetPageData(vue);
    },
    watch: {
        "pageModel.pageSize": function (val) {
            this.pageModel.pageIdx = 1;
            ResetPageData(this);
        }
    }
});

/**
 * 根据条件显示页面数据
 */
function ResetPageData(vue) {
    var showEquipment = GetQueryString("ShowEquipment");
    var showMaintainRecord = GetQueryString("ShowMaintainRecord");
    
    if (showEquipment) {
        ListEquipmentByAssignerIDForPage(vue);
    }

    if (showMaintainRecord) {
        GetMaintainRecordByAssignerID(vue)
    }
}

/**
 * 根据负责人ID查询所属所有设备
 */
function ListEquipmentByAssignerIDForPage(vue) {
    vue.equipmentList = [];
    vue.pageModel.results = [];
    ShowAzLoading();
    $.ajax({
        type: "POST",
        url: "/Equipment/ListEquipmentByAssignerIDForPage",
        data: {
            pageModel: vue.pageModel,
            searchModel: vue.searchModel
        },
        success: function (res) {
            if (res.status == "Success") {
                vue.pageModel = res.data;
                vue.equipmentList = vue.pageModel.results;
                ResetEquipmentDateFormat(vue);
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("获取设备信息失败，请稍候再试Failed to get device information. Please try again later。");
        }
    });
}

/**
 * 根据负责人查询所有的保养记录
 */
function GetMaintainRecordByAssignerID(vue) {
    vue.maintainRecordList = [];
    ShowAzLoading();
    $.ajax({
        type: "GET",
        url: "/Maintain/ListMaintainRecordsByCheckerID?CheckerID=" + GlobalUser.getUserInfo().ID + "&v=" + Math.random(),
        success: function (res) {
            if (res.status == "Success") {
                for (var x in res.data) {
                    console.log(res.data[x].BaseInfo.EquipmentCode)
                }

                vue.maintainRecordList = res.data;
                ResetMaintainRecordDateFormat(vue);
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("获取保养记录失败，请稍候再试Failed to obtain maintenance record. Please try again later。");
        }
    })
}

/**
 * 编辑设备信息
 */
function EditEquipmentDetail(vue, equipmentID) {
    OpenAzModal("编辑设备信息Edit device information", "/Equipment/Page/Detail?EquipmentID=" + equipmentID, function () {
        ResetPageData(vue);
    });
    ResizeAzModal(1000);
}

/**
 * 重置设备列表信息中时间显示问题
 */
function ResetEquipmentDateFormat(vue) {
    for (var idx in vue.equipmentList) {
        vue.equipmentList[idx].BaseInfo.PurchaseDate = CSharpToJsDate(vue.equipmentList[idx].BaseInfo.PurchaseDate);
        vue.equipmentList[idx].BaseInfo.WarrantyDate = CSharpToJsDate(vue.equipmentList[idx].BaseInfo.WarrantyDate);
        vue.equipmentList[idx].MeasureInfo.WarnMeasureDate = CSharpToJsDate(vue.equipmentList[idx].MeasureInfo.WarnMeasureDate);
        vue.equipmentList[idx].MeasureInfo.LastMeasureDate = CSharpToJsDate(vue.equipmentList[idx].MeasureInfo.LastMeasureDate);
        vue.equipmentList[idx].MeasureInfo.NextMeasureDate = CSharpToJsDate(vue.equipmentList[idx].MeasureInfo.NextMeasureDate);
        vue.equipmentList[idx].MaintainInfo.WarnMaintainDate = CSharpToJsDate(vue.equipmentList[idx].MaintainInfo.WarnMaintainDate);
        vue.equipmentList[idx].MaintainInfo.LastMaintainDate = CSharpToJsDate(vue.equipmentList[idx].MaintainInfo.LastMaintainDate);
        vue.equipmentList[idx].MaintainInfo.NextMaintainDate = CSharpToJsDate(vue.equipmentList[idx].MaintainInfo.NextMaintainDate);
        vue.equipmentList[idx].MsaInfo.WarnMsaDate = CSharpToJsDate(vue.equipmentList[idx].MsaInfo.WarnMsaDate);
        vue.equipmentList[idx].MsaInfo.LastMsaDate = CSharpToJsDate(vue.equipmentList[idx].MsaInfo.LastMsaDate);
        vue.equipmentList[idx].MsaInfo.NextMsaDate = CSharpToJsDate(vue.equipmentList[idx].MsaInfo.NextMsaDate);
    }
}

/**
 * 重置保养记录中时间显示问题
 */
function ResetMaintainRecordDateFormat(vue) {
    for (var idx in vue.maintainRecordList) {
        vue.maintainRecordList[idx].BaseInfo.RecordDate = CSharpToJsDate(vue.maintainRecordList[idx].BaseInfo.RecordDate);
        vue.maintainRecordList[idx].BaseInfo.UpdateDate = CSharpToJsDate(vue.maintainRecordList[idx].BaseInfo.UpdateDate);
    }
}

/**
 * 添加保养记录
 */
function AddMaintainFormRecord(vue, equipmentID) {
    var url = "/Maintain/Page/RecordDetail?IsNew=true&EquipmentID=" + equipmentID + "&CheckerID=" + GlobalUser.getUserInfo().ID;
    OpenAzModal("新增保养记录Add maintenance record", url, function () {
        ResetPageData(vue);
    });
}

/**
 * 编辑保养记录
 */
function EditMaintainFormRecord(vue, recordID) {
    var url = "/Maintain/Page/RecordDetail?IsNew=false&RecordID=" + recordID;
    OpenAzModal("编辑保养记录Edit maintenance record", url, function () {
        ResetPageData(vue);
    });
}

//批量删除保养记录
function BatchDeleteMaintainFormRecord(vue) {
    if (vue.batchRecord.recordIdx < vue.batchRecord.recordIDs.length) {
        var recordID = vue.batchRecord.recordIDs[vue.batchRecord.recordIdx];
        DeleteMaintainFormRecord(vue, recordID, true);
    } else {
        toastr.success("成功删除Successfully deleted[" + vue.batchRecord.recordIdx + "]条保养记录Maintenance records。");
        //ResetPageData(vue);
    }
}


/**
 * 删除保养记录
 */
function DeleteMaintainFormRecord(vue, recordID, isBatch) {
    ShowAzLoading();
    $.ajax({
        type: "POST",
        url: "/Maintain/DeleteMaintainRecord",
        data: { RecordID: recordID },
        success: function (res) {
            if (res.status == "Success") {
                if (!isBatch) {
                    toastr.success("删除保养记录信息成功Successfully deleted maintenance record information。");
                    //ResetPageData(vue);
                } else {
                    vue.batchRecord.recordIdx++;
                    BatchDeleteMaintainFormRecord(vue);
                }

                //操作成功后删除元素
                for (var idx = vue.maintainRecordList.length - 1; idx >= 0; idx--) {
                    if (vue.maintainRecordList[idx].BaseInfo.ID == recordID) {
                        vue.maintainRecordList.splice(idx, 1);
                    }
                }
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            HideAzLoading();
        }
    });
}

//批量提交保养记录
function BatchSubmitMaintainFormRecord(vue) {
    if (vue.batchRecord.recordIdx < vue.batchRecord.recordIDs.length) {
        var recordID = vue.batchRecord.recordIDs[vue.batchRecord.recordIdx];
        SubmitMaintainFormRecord(vue, recordID, true);
    } else {
        toastr.success("成功提交Successfully submitted[" + vue.batchRecord.recordIdx + "]条保养记录Maintenance records。");
        //ResetPageData(vue);
    }
}

/**
 * 提交保养记录签核
 */
function SubmitMaintainFormRecord(vue, recordID, isBatch) {
    ShowAzLoading();
    $.ajax({
        type: "POST",
        url: "/Maintain/SubmitMaintainRecord",
        data: { recordID: recordID },
        success: function (res) {
            if (res.status == "Success") {
                if (!isBatch) {
                    toastr.success("提交保养记录Submit maintenance records[" + recordID + "]单成功Single success");
                    //ResetPageData(vue);
                } else {
                    vue.batchRecord.recordIdx++;
                    BatchSubmitMaintainFormRecord(vue);
                }

                //提交成功后删除元素
                for (var idx = vue.maintainRecordList.length - 1; idx >= 0; idx--) {
                    if (vue.maintainRecordList[idx].BaseInfo.ID == recordID) {
                        vue.maintainRecordList.splice(idx, 1);
                    }
                }
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("提交保养记录失败，请稍候再试Failed to submit maintenance record. Please try again later。");
        }
    });
}

/**
 * 查询签核保养记录
 */
function ApproveMaintainFormRecord(vue, recordID) {
    var url = "/Home/Approval?RecordID=" + recordID;
    window.location.href = url;
}